package com.tdam2011.grupo02.ui;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.compatibility.ActionBarActivity;
import com.tdam2011.grupo02.connectivity.ConnectivityInformationSingleton;
import com.tdam2011.grupo02.data.OnSendingInterface;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MensajeWebComposicionActivity extends ActionBarActivity implements
		OnSendingInterface {

	private EditText destinatario;
	private EditText mensaje;
	private NotificationManager notificationManager;
	private int NOTIFICATION = R.string.sendMsgwNotificationTitle;
	private Notification notification;

	private static final int ID_NO_CONNECTION = 1;
	public static final String ACTION = "com.tdam2011.grupo02.ui.SEND_MESSAGE_ERROR";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mensajeweb_composicion);

		setTitle(R.string.txtComposicionTitle);

		destinatario = (EditText) findViewById(R.id.msgWebDestinatario);
		mensaje = (EditText) findViewById(R.id.msgWebMensaje);
		Button enviar = (Button) findViewById(R.id.btnMsgWebEnviar);
		Button cancelar = (Button) findViewById(R.id.btnMsgWebCancelar);

		if (getIntent().hasExtra("destinatario"))
			destinatario.setText(getIntent().getExtras().getString(
					"destinatario"));
		if (getIntent().hasExtra("mensaje"))
			mensaje.setText(getIntent().getExtras().getString("mensaje"));

		enviar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendWebMessage();
			}
		});

		cancelar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		if (id == ID_NO_CONNECTION) {
			dialog = createNoConnectionDialog();
		} else {
			dialog = super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}

	private Dialog createNoConnectionDialog() {
		Dialog dialog = new AlertDialog.Builder(this)
				.setIcon(R.drawable.quick_action_msgweb)
				.setTitle(getString(R.string.connectionDialogTitle))
				.setPositiveButton(getString(R.string.alertBtnOk),
						null)
				.setMessage(getString(R.string.msgNoInternetConnection))
				.create();
		return dialog;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;

		switch (item.getItemId()) {
		case android.R.id.home:
			intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void sendWebMessage() {
		if (ConnectivityInformationSingleton
				.getInstance(MensajeWebComposicionActivity.this)
				.getConnectivityInformation().isInternetConnectionAvailable()) {

			ContactActionControllerSingleton
					.getInstance(MensajeWebComposicionActivity.this)
					.getContactActionController()
					.sendMsgWebConnection(this, destinatario.getText().toString(),
							mensaje.getText().toString(), false);
		} else {
			showDialog(ID_NO_CONNECTION);
			
			ContactActionControllerSingleton
			.getInstance(MensajeWebComposicionActivity.this)
			.getContactActionController()
			.sendMsgWebNoConnection(this, destinatario.getText().toString(),
					mensaje.getText().toString());		
			
			finish();			
		}
	}

	// Notifications

	private void showNotificationSending() {
		notification = new Notification(R.drawable.quick_action_msgweb_sending,
				getString(R.string.sendMsgwNotificationTitle),
				System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(
				getApplicationContext(), 0, new Intent(),
				PendingIntent.FLAG_UPDATE_CURRENT);

		notification.setLatestEventInfo(this,
				getString(R.string.sendingMsgwNotificationMessage),
				getString(R.string.sendMsgwNotificationTitle), contentIntent);

		notification.flags = Notification.FLAG_ONGOING_EVENT;

		notificationManager.notify(NOTIFICATION, notification);
	}

	private void updateNotificationSent() {
		Intent intent = new Intent(this, MsgwConversationListActivity.class);
		intent.putExtra("name", destinatario.getText().toString());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_SOUND;

		notification.icon = R.drawable.quick_action_msgweb_sent_succes;
		notification.setLatestEventInfo(this,
				getString(R.string.sentMsgwNotificationMessage),
				getString(R.string.sendMsgwNotificationTitle), contentIntent);

		notificationManager.notify(NOTIFICATION, notification);
	}

	private void updateErrorNotificationSent() {
		PendingIntent contentIntent = PendingIntent.getActivity(
				getApplicationContext(), 0, new Intent(),
				PendingIntent.FLAG_UPDATE_CURRENT);

		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_SOUND;

		notification.icon = R.drawable.quick_action_msgweb_sent_error;
		notification.setLatestEventInfo(this,
				getString(R.string.sentMsgwNotificationErrorMessage),
				getString(R.string.sendMsgwNotificationTitle), contentIntent);

		notificationManager.notify(NOTIFICATION, notification);
	}

	@Override
	public void onSending() {
		showNotificationSending();
	}

	@Override
	public void onSuccess() {
		updateNotificationSent();
		finish();
	}

	@Override
	public void onError() {
		updateErrorNotificationSent();
	}

}
